<template>
  <el-container class='main-container'>
    <el-aside class='aside-menu mt15'>
      <p class='text-lg'>
        <a href='javascript:void(0);'>博客人生</a>
      </p>
      <ul class='menu mt15'>
        <li v-for='menu in amenus' :key='menu.name' :class='{ selected: menu.name == menuSelected.name }' @click='select($event, menu)'>
          <a href='javascript:void(0);' :name='menu.name'>{{ menu.label }}</a>
        </li>
      </ul>
    </el-aside>
    <el-main class='main-content mt15'>
      <p class='position tl'>
        您现在的位置
        <a href='javascript:void(0);'>首页</a>&nbsp;&nbsp;>&nbsp;&nbsp;
        <a href='javascript:void(0);'>{{ menuSelected.label }}</a>
      </p>
      <router-view />
    </el-main>
  </el-container>
</template>
<script>
import { useRouter, useRoute } from 'vue-router';
import { ref, reactive } from 'vue';
import { menus } from '@/utils/sdata.js';

export default {
  name: 'Main',
  setup () {
    const router = useRouter();
    const amenus = ref(menus);
    const route = useRoute();
    const menuSelected = reactive({
      name: 'psnInfo',
      label: '个人信息',
    });

    let hash = window.location.hash;
    let hashPath = hash.substring(1);
    if (hashPath.length > 1)    {
      let curRouter = router.getRoutes().find((item) => item.path == hashPath);
      let curMenu = menus.find(
        (item) => curRouter.meta && curRouter.meta.menu == item.name
      );
      if (curMenu)      {
        menuSelected.name = curMenu.name;
        menuSelected.label = curMenu.label;
      }
    }

    const select = (e, smenu) => {
      const { name, label } = smenu;
      menuSelected.name = name;
      menuSelected.label = label;

      router.push({ name: name });
    };
    return {
      amenus,
      menuSelected,
      select,
    };
  },
};
</script>

<style lang="scss" scoped>
.main-container {
  width: 1100px;
  margin: 0 auto;
  margin-top: 60px;

  .aside-menu {
    width: 150px;
    text-align: center;
  }
  .text-lg {
    color: #ed4040;
    font-size: 30px;
    font-style: normal;
    font-weight: bolder;
    font-family: cursive;
  }

  .menu li {
    height: 40px;
    line-height: 40px;
    margin-bottom: 2px;
    color: #444;
    border-radius: 4px;
    overflow: hidden;
  }

  .menu li a {
    display: block;
  }
  .menu li.selected a,
  .menu li:hover a {
    background-color: #ed4040;
    color: #fff;
  }
  .main-content {
    margin-left: 30px;
    padding: 0;
  }
  .main-content .position {
    font-size: 14px;
    font-weight: normal;
    margin-bottom: 10px;
    position: relative;
    border-top: #ccc 2px solid;
    // border-bottom: #ececec 1px solid;
    color: #888;
    line-height: 40px;
  }
  .main-content .position:after {
    position: absolute;
    width: 100px;
    height: 2px;
    background: #ed4040;
    content: '';
    left: 0;
    top: -2px;
  }
}
</style>
